﻿Public Class POInfo

    Private m_TableName As String
    Private m_columns As ArrayList


    Public Sub New(ByVal mTableName As String, ByVal b As Boolean)
        Me.m_TableName = mTableName
        Me.m_columns = New ArrayList
        loadInfo(b)
    End Sub

    ''' <summary>
    ''' Load giá trị cho m_columns
    ''' </summary>
    ''' <param name="b"></param>
    ''' <remarks></remarks>
    Public Sub loadInfo(ByVal b As Boolean)
        '' Dùng câu truy vấn đề lấy thông tin về các column: tên, type, ....
        Dim sql As String
        sql = "SELECT COLUMN_NAME, DATA_TYPE, COLUMN_TYPE, IS_NULLABLE, COLUMN_KEY"
        sql += " FROM INFORMATION_SCHEMA.COLUMNS"
        sql += " WHERE TABLE_SCHEMA = '" & Config.TABLE_SCHEMA & "' AND TABLE_NAME = '" & Me.m_TableName & "'"

        Dim myConnect As MySqlConnect
        myConnect = New MySqlConnect

        Dim da As DataTable
        da = New DataTable
        da = myConnect.executeQuery(sql)

        If (da.Rows.Count = 0) Then
            Return
        End If

        Dim i As Integer
        For i = 0 To da.Rows.Count - 1
            Dim Column_Name As String = CType(da.Rows(i)("COLUMN_NAME"), String)
            Dim Data_Type As String = CType(da.Rows(i)("DATA_TYPE"), String)
            Dim Column_Type As String = CType(da.Rows(i)("COLUMN_TYPE"), String)
            Dim Is_Nullable As String = CType(da.Rows(i)("IS_NULLABLE"), String)
            Dim Column_Key As String = CType(da.Rows(i)("COLUMN_KEY"), String)

            Dim poi As POInfoColumn
            poi = New POInfoColumn(Column_Name, Data_Type, Column_Type, Is_Nullable, Column_Key)
            Me.m_columns.Add(poi)
        Next


    End Sub


    Public Property TableName() As String
        Get
            Return Me.m_TableName
        End Get
        Set(ByVal value As String)
            Me.m_TableName = value
        End Set
    End Property


    Public Function columnCount() As Integer
        Return Me.m_columns.Count
    End Function

    Public Function getPOInfoColumn(ByVal col As Integer) As POInfoColumn
        Return Me.m_columns(col)
    End Function

    Public Function getKey() As String
        Dim i As Integer = 0
        For i = 0 To Me.columnCount - 1
            If (Me.getPOInfoColumn(i).getColumnKey.Equals(CONSTRAINT.PRIMARY)) Then
                Return Me.getPOInfoColumn(i).getColumnName
            End If
        Next
        Return Nothing
    End Function

End Class
